package com.zsy.provider.web.rpc;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zsy.base.bean.DemoResult;
import com.zsy.config.SpringCloudProperties;
import com.zsy.config.properties.UacProperties;
import com.zsy.dao.commonmaper.CommonUserMapper;
import com.zsy.dao.model.AosRmsUser;
import com.zsy.dao.model.CgsDksq;
import com.zsy.dao.model.TbPermission;
import com.zsy.dao.model.TbUser;
import com.zsy.provider.api.service.UacService;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

@RestController
@RefreshScope
@Slf4j
public class UacUserFeignClient implements UacService {

    @Value("${zhaoshouyun}")
    String zhaoshouyun;
    @Value("${spring.profiles.active}")
    String profileActive;
    @Value("${server.port}")
    String serverPort;

    //不建议使用这个
    @Resource
    UacProperties uacProperties;
    //建议使用这种
    @Resource
    SpringCloudProperties springCloudProperties;



    @Autowired
    CommonUserMapper commonUserMapper;

    @Override
    public DemoResult<Page<CgsDksq>> listCgsDksq(JSONObject jsonObject) {
        CgsDksq cgsDksq = new CgsDksq();
        QueryWrapper<CgsDksq> wrapper = new QueryWrapper();
        wrapper.lambda()
                .eq(StringUtils.isNotBlank(jsonObject.getString("fpdm")),CgsDksq::getFpdm,jsonObject.getString("fpdm"))
                .eq(StringUtils.isNotBlank(jsonObject.getString("fphm")),CgsDksq::getFphm,jsonObject.getString("fphm"))
                .like(StringUtils.isNotBlank(jsonObject.getString("nsrmc")),CgsDksq::getNsrmc,jsonObject.getString("nsrmc"))
                .or().like(StringUtils.isNotBlank(jsonObject.getString("clcd")),CgsDksq::getClcd,jsonObject.getString("clcd"));

       // wrapper.like("name", "雨").lt("age", 40);

        Page<CgsDksq> page = new Page<>(1,10);

        IPage<CgsDksq> page1 = cgsDksq.selectPage(page, wrapper);
        log.info(String.valueOf(page1.getRecords().size()));
        return DemoResult.SUCCESS(page1);
    }

    /**
     * 获取用户列表
     *
     * @param id
     * @return
     */
    @Override
    public DemoResult<Object> listUser(String id) {
        AosRmsUser aosRmsUser = new AosRmsUser();

        /*********************测试配置从公共工具取数据 start*******************************/
        log.info("uacProp:{},,springCloudProperties:{}", JSON.toJSONString(uacProperties),springCloudProperties.demoProperties().getDemo());
        /*********************测试配置从公共工具取数据 end*******************************/
        log.info("******************************************************server.port:{},spring.profiles.active:{},zhaoshouyun:{}",serverPort,profileActive,zhaoshouyun);
        if (StringUtils.isBlank(id)){
            //id = id.substring(2);
            List<AosRmsUser> aosRmsUsers = aosRmsUser.selectAll();
            return DemoResult.SUCCESS(zhaoshouyun,aosRmsUsers);
        }
        return DemoResult.SUCCESS(zhaoshouyun,aosRmsUser.selectById(id));
    }


    /**
     * @param userName
     * @return com.zsy.base.bean.DemoResult<java.lang.Object>
     * @Author zhaoshouyun
     * @Description 获取用户信息
     * @Date 2020-04-15 05:15
     * @Param userName
     */
    @Override
    public DemoResult<TbUser> getUserByUserName(String userName) {
        log.info("用户名：{}",userName);
        TbUser tbUser = new TbUser();
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_Name",userName);
        TbUser tbUser1 = tbUser.selectOne(wrapper);
        return DemoResult.SUCCESS( tbUser1);
    }

    /**
     * 通过用户名更新用户手机号，为了测试事务
     *
     * @param userName
     * @param phone
     * @return com.zsy.base.bean.DemoResult<com.zsy.dao.model.TbUser>
     * @Author zhaoshouyun
     * @Description
     * @Date 2020-04-29 01:55
     * @Param userName
     * @Param phone
     */
    /**
     *
     spring对于事务异常的处理

     unchecked   运行期Exception   spring默认会进行事务回滚       比如：RuntimeException
     checked       用户Exception       spring默认不会进行事务回滚   比如：Exception
     如何改变spring的这种默认事务行为？可以通过在方法上

     添加@Transactional(noRollbackFor=RuntimeException.class)让spring对于RuntimeException不回滚事务

     添加@Transactional(RollbackFor=Exception.class)让spring对于Exception进行事务的回滚

     在项目中，@Transactional(rollbackFor=Exception.class)，如果类加了这个注解，那么这个类里面的方法抛出异常，就会回滚，数据库里面的数据也会回滚
      **/
    @Transactional(rollbackFor = {Exception.class})
    @Override
    public DemoResult<TbUser> updateUserByUserName(String userName, String phone) {
        log.info("用户名：{}",userName);
        TbUser tbUser = new TbUser();
        tbUser.setPhone(phone);
        QueryWrapper<TbUser> wrapper = new QueryWrapper<>();
        wrapper.eq("user_Name",userName);

        //TbUser tbUser1 = tbUser.selectOne(wrapper);
        boolean update = tbUser.update(wrapper);

        log.info("{}---用户信息更新：{}",userName,update);
        return getUserByUserName(userName);
    }

    /**
     * 获取权限列表
     *
     * @param id
     * @return com.zsy.base.bean.DemoResult<com.zsy.dao.oauth2.model.TbPermission>
     * @Author zhaoshouyun
     * @Description
     * @Date 2020-04-15 05:36
     * @Param id
     */
    @Override
    public DemoResult<List<TbPermission>> selectPermissionsByUserId(BigDecimal id) {

        List<TbPermission> tbPermissions = commonUserMapper.listTbPermission(id);
        return DemoResult.SUCCESS(tbPermissions);

    }
}
